<!DOCTYPE html>
<html>
<head>
<title>discounts</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<meta name="viewport" id="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no"/>
<link href="/static/webAPP/css/reset.css" rel="stylesheet" type="text/css">
<link href="/static/webAPP/css/discounts.css" rel="stylesheet" type="text/css">
<script src="/static/webAPP/js/jquery-1.8.3.min.js"></script>
</head>
<body>
<div class="near-box">
      <!--头部开始-->
	  <div class="header">
	  	 <a href="./index.html" class="left"></a>
	  	 <span class="discountsName"></span>
	  </div>
	  <!--头部结束-->
      <div class="discounts-bigbox discounts_info">
		  
	  </div>
      <!--bigbox结束-->
      <div class="discounts-bottom">
		  
      </div>
      
</div>
<script src="/static/webAPP/lib/mobile/layer.js"></script>
<script type="text/javascript" src="js/scrolltopcontrol.js"></script>
<script>
	$(function(){
		var discounts = getUrlParam('discounts')
		var discountsName = getUrlParam('discountsName')
		$(".discountsName").html(decodeURI(discountsName))
		var tag = ''
		$.ajax({
			type: "get",
						//TODO:
			url: "/app/commodityInfo/getCommodityInfoByCategory/"+ discounts +"/1",
			async : false,
			data: '',
			dataType: "json",
			success: function(mag){
				// console.log(mag)
				var data = mag.data
				// var tag = ''
				for (let i = 0; i < data.length; i++) {
					var tag1 = '<div class="discounts-box1"><a href="./goods-details.html?id='+ data[i].id +'" class="d-box1-img1"><img src="https://vm.fowotek.com/'+ data[i].picture[0].picture_url +'" alt=""></a><div class="discounts-box2" cid="'+ data[i].id +'" specificationsid="'+ data[i].specifications[0].id +'"><span class="d-box2-text1">'+ data[i].commodityName +'</span><span class="d-box2-text3">'+ data[i].specifications[0].specifications +'</span><span class="d-box2-text2">￥'+ data[i].specifications[0].price +'</span><span class="d-box2-btn"><img src="./images/shop-cart1.png"></span></div></div>'
					
					tag += tag1
				}
				$(".discounts_info").html(tag)
			}
		})



		
		$(document).on('click','.d-box2-btn',function(){
			var id = $(this).closest(".discounts-box2").attr("cid")
			//TODO:加入购物车
			var userName = sessionStorage.getItem("sessionName")
			var specificationsid = $(this).closest(".discounts-box2").attr("specificationsid")
			if (userName == null || userName == '' || userName == undefined) {
				layer.open({
					content: '请先去登录账号',
					skin: 'msg',
					time: 2 //2秒后自动关闭
				});
			}else{
				$.ajax({
					type: "get",
										//TODO:
					url: "/app/userInfo/" + userName,
					data: '',
					dataType: "json",
					success: function(mag){
						$.ajax({
							type: "post",
									//TODO:
							url: "/app/basket/addCommodityToBasket",
							data: {commodity_id: id,commodity_specification_id: specificationsid,count: 1,user_id: mag.data.id},
							dataType: "json",
							success: function(res){
								layer.open({
									content: res.message,
									skin: 'msg',
									time: 2 //2秒后自动关闭
								});
							}
						})
					},
					error: function(){
						layer.open({
							content: '登录状态失效，请重新登录',
							skin: 'msg',
							time: 2 //2秒后自动关闭
						});
					}
				})
			}
		})


		//下拉加载更多
		var num = 1
		$(window).scroll(function(){
			var scrollTop = $(this).scrollTop();    
			var scrollHeight = $(document).height();   
			var clientHeight = $(this).height();    
			if(scrollTop + clientHeight >= scrollHeight){
				num ++
				// console.log(num)
				$.ajax({
					type: "get",
							//TODO:
					url: "/app/commodityInfo/getCommodityInfoByCategory/"+ discounts +"/"+ num,
					data: '',
					dataType: "json",
					success: function(mag){
						var data = mag.data
						for (let i = 0; i < data.length; i++) {
							var tag1 = '<div class="discounts-box1"><a href="./goods-details.html?id='+ data[i].id +'" class="d-box1-img1"><img src="https://vm.fowotek.com/'+ data[i].picture[0].picture_url +'" alt=""></a><div class="discounts-box2" cid="'+ data[i].id +'" specificationsid="'+ data[i].specifications[0].id +'"><span class="d-box2-text1">'+ data[i].commodityName +'</span><span class="d-box2-text3">'+ data[i].specifications[0].specifications +'</span><span class="d-box2-text2">￥'+ data[i].specifications[0].price +'</span><span class="d-box2-btn"><img src="./images/shop-cart1.png"></span></div></div>'
							tag += tag1
						}
						if (data.length != 10) {
							$(".discounts_info").html(tag + '<div style="text-align: center;color: #ccc;">已加载全部商品</div>')
						}else{
							$(".discounts_info").html(tag)
						}
					}
				})
			}
		});
	})


	function getUrlParam(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
		var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
		var context = "";
		if (r != null){
			context = r[2];
			reg = null;
			r = null;
		}
		return context == null || context == "" || context == "undefined" ? "" : context;
	}
</script>
</body>
</html>